package TUILayer;
import ControlLayer.*;
import ModelLayer.*;
import java.util.Scanner;

/**
 * This is the 4th menu, the Loan one.
 *
 * @author (Group4) 
 * @version (2)
 */

public class LoanUI
{
    // instance variables    
    private LoanCtr controler;

    public LoanUI()
    {
        controler=new LoanCtr();
        
    }

    // starts the Loan Menu
    public void LoanMenu()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = textLoanMenu();
            switch (choise)
            {
                case 1:
                {
                    //check if a person it's in the system and if there is a copy of a dvd avaiable
                    String i="";
                    while (!i.equals("1"))
                    {
                        Scanner keyboard = new Scanner(System.in);  
                        System.out.println("Please type the user id that you want more info:");
                        String DVDTitle=keyboard.nextLine();

                        System.out.println("Please type dvd title that you want to check:");
                        String personId=keyboard.nextLine();

                        controler.checkIfAvailable(DVDTitle,personId); 

                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to check again.");
                        String var=keyboard.nextLine();
                        i=var;
                    }   
                    break;
                }
                case 2:
                {
                    // create a loan
                    String i="";
                    while (!i.equals("1"))
                    {   
                        Scanner keyboard = new Scanner(System.in);
                        createLoan();

                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to check another loan.");
                        String var=keyboard.nextLine();
                        i=var;
                    }   
                    break;

                }
                case 3:
                {
                    { 
                        // check if a person have a loan and return the info:
                        String i="";
                        while (!i.equals("1"))
                        {   

                            Scanner keyboard = new Scanner(System.in);  
                            System.out.println("Please type the person id of the person you want to check the loan:");
                            String personId=keyboard.nextLine();

                            controler.readLoan(personId);

                            System.out.println("Press 1 to return");
                            System.out.println("Press enter to check another loan.");
                            String var=keyboard.nextLine();
                            i=var;

                        }   
                        break;  
                    }
                }
                case 4:
                {
                    String i="";
                    while (!i.equals("1"))
                    {   
                        // update info about a loan
                        updateLoan();

                        Scanner keyboard = new Scanner(System.in); 
                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to update another loan.");
                        String var=keyboard.nextLine();
                        i=var;

                    }   
                    break;  
                }
                case 5:
                {
                    //delete loan
                    String i="";
                    while (!i.equals("1"))
                    {   
                        Scanner keyboard = new Scanner(System.in);
                        System.out.println("Press enter person's id that you want to delete loan:");
                        String personId=keyboard.nextLine();

                        controler.deleteLoan(personId);

                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to update another loan.");
                        String var=keyboard.nextLine();
                        i=var;

                    }  break;}
                case 6:
                {
                    
                    String i="";
                    while (!i.equals("1"))
                    {   
                        Scanner keyboard = new Scanner(System.in);
                        System.out.println("Press enter loan id that you want to return:");
                        String loanId=keyboard.nextLine();

                        System.out.println("Press enter serial number of the copy:");
                        int serialNumber=keyboard.nextInt();

                        controler.returnLoan(loanId,serialNumber);

                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to return another loan.");
                        keyboard.nextLine();

                        String var=keyboard.nextLine();

                        i=var;
                    } 
                    break;}

                case 7:
                {
                    //return to main menu
                    exit = true;
                    break;
                }

            }
        }
    }

    
    // display what info you should type in order to upload a loan
    public void updateLoan()
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write new person Id:");
        String personId=keyboard.nextLine();

        System.out.println(" Write new borrow date:");
        String borrowDate=keyboard.nextLine();

        System.out.println(" Write new  date:");
        String period=keyboard.nextLine();

        System.out.println(" Write new copy id");
        String copyID=keyboard.nextLine();

        controler.updateLoan(personId,borrowDate,period,copyID);
    }

    // the menu for uploading a loan
    private int textLoanMenu()
    {       
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1)Check if the copy available");
        System.out.println(" (2)Create loan");
        System.out.println(" (3)Read loan");
        System.out.println(" (4)Update loan");
        System.out.println(" (5)Delete loan");
        System.out.println(" (6)Return loan");
        System.out.println(" (7)Return");
        System.out.print("\n Make your choise: ");

        int choise = keyboard.nextInt();

        return choise;
    }  
    
    // display what info you should type in order to create a loan
    public void createLoan()
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write DVD title:");
        String DVDTitle=keyboard.nextLine();

        System.out.println(" Write personal id:");
        String personId=keyboard.nextLine();

        System.out.println(" Write the borrow date:");
        String borrowDate=keyboard.nextLine();

        System.out.println(" Write the period:");
        String period=keyboard.nextLine();

        System.out.println(" Write the person id");
        String personid=keyboard.nextLine();

        System.out.println(" Write the loan Id");
        String loanId=keyboard.nextLine();

        System.out.println(" Write the copy Id");
        String copyId=keyboard.nextLine();

        System.out.println(" Write serial number");
        int serialNumber=keyboard.nextInt();

        controler.createLoan(DVDTitle,personId,borrowDate,period,personid,loanId,copyId,serialNumber);
    }
}

